package com.crr.servlet;

import com.crr.mapper.AdminMapper;
import com.crr.mapper.EmpMapper;
import com.crr.pojo.Admin;
import com.crr.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SqlSession sqlSession= SqlSessionFactoryUtil.getSqlSession();
        AdminMapper am =sqlSession.getMapper(AdminMapper.class);

        String username=req.getParameter("username");
        String password=req.getParameter("password");
        Admin admin=new Admin();
        admin.setAname(username);
        admin.setApwd(password);
       Admin a= am.adminLogin(admin);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        if (a!= null) {

            out.println("<script> alert('登录成功'); location.href='main.jsp'</script>");

        } else {
            out.println("<script> alert('登录失败'); location.href='login.html'</script>");
        }
    }


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
